<?php
namespace app\index\controller;
use think\Db;

class Index extends Common
{
    public function index()
    {   
    	$bid = session("bid");
    	$book = db('book')->where(['id'=>$bid])->field('*')->find();

    	if(empty($bid))$this->error('页面已过期!');
    	if(request()->isAjax()){
	    	if(!empty($book) && !empty($bid)){
		    	if($book['user_id'] != $this->info['id']){
		    		$update = [];
		    		if(!empty($book['users'])){
		    			$users = explode(",", $book['users']);
		    			if(!in_array($this->info['id'],$users)){
		   
		    				$users[] = 	$this->info['id'];
		    				$update['users'] = implode(",", $users);
		    			}
		    		}else{
		    			$update['users'] = $this->info['id'];
		    		}

		    		db('book')->where(['id'=>$bid])->update($update);
		    		session("bid",null);
		    	}
	    	}
    	}

    	$mid = db('member')->where(['id'=>$book['user_id']])->field('mid,nickname')->find();
    	$path = db('img_list')->where(['id'=>$mid['mid']])->value('path');
    	$book_path = db('img_list')->where(['id'=>$book['mid']])->value('path');
    	$path = IMG_PATH.$path;
    	$book_path = IMG_PATH.$book_path;
    	$book['user_head'] = $path;
    	$book['nickname'] = $mid['nickname'];
    	$book['mid'] = $book_path;

    	$this->assign('book',$book);
		return $this->fetch();
	}
}
